//
//  MainTabController.swift
//  Mircoblog-swift
//
//  Created by zz on 16/2/23.
//  Copyright © 2016年 zz. All rights reserved.
//

import UIKit

class MainTabController: UITabBarController {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        showNewFeature()
        
        //在viewdidLoad方法里，tabBarButton还没创建，因此在这边以下方法不起作用
//        tabBar.bringSubviewToFront(button) //unuse
   
       
        addControllers()
        
        
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
        
        setUpTabBar()
        tabBar.bringSubviewToFront(button)
       
    }

    // MARK: - 私有控件
    private lazy var button = UIButton(zd_title: nil, imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
//MARK: - 设置新界面
extension MainTabController{
    /// 显示新特性
    private func showNewFeature(){
        // 1. 如果没有登录直接返回
        if !UserAccount.sharedUserAccount.isLogin{
            return
        }
        // 2. 判断是否有新版本 
        let v = isNewVersion ? NewFeatureView() : WelcomeView()
        // 3. 将视图添加到当前视图
        view.addSubview(v)
    }
    
    /// 是否有新版本
    private var isNewVersion: Bool{
    
        // 1. 获取当前版本
        let currentVersion = NSBundle.zd_currentVersion
        // 2. 获取`之前`的版本，保存在用户偏好
        let versionKey = "versionKey"
        let oldVersion = NSUserDefaults.standardUserDefaults().stringForKey(versionKey)
        // 3. 将当前版本保存在用户偏好
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: versionKey)
        print("是否是welcomeView\(currentVersion != oldVersion)")
        return currentVersion != oldVersion
    }
}
//MARK: - 设置tabBar
extension MainTabController{

    private func setUpTabBar(){
    
        // 计算按钮宽度
        // -1 是防止边界处理
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
        // CGRectInset Returns a rectangle that is smaller or larger than the source rectangle
        // > 0，向内缩小
        // < 0，向外
        button.frame = CGRectInset(tabBar.bounds, width * 2, 0)

        button.addTarget(self, action: "clickAdd", forControlEvents: .TouchUpInside)
        
        tabBar.addSubview(button)
        
        
        // 设置tabbar背景图片，以下两句代码必须连用，Nav 的处理类似
        tabBar.shadowImage = UIImage.cz_singleDotImage(UIColor(white: 0.5, alpha: 1.0))//用点可以设置成线
        
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        // 如果两个属性都设置成 [[UIImage alloc] init] 就是完全透明
        //        tabBar.shadowImage = UIImage()
        //        tabBar.backgroundImage = UIImage()
        // 如果提供图像，会自动拉伸
        
    }
    @objc private func clickAdd(){
        
        let transitonAnimatorVc = TranstionViewController(a: 5)
        transitonAnimatorVc.modalPresentationStyle = .Custom
        presentViewController(transitonAnimatorVc, animated: true, completion: nil)
    
    /*
        let nav: UINavigationController = UINavigationController(rootViewController: ComposeViewController())
        presentViewController(nav, animated: true, completion: nil)
    */
        
    }
}

// MARK: - 添加控制器
// 通过 extension 把相关的代码进行分组放置
extension MainTabController{

    private func addChildViewController(control: UIViewController,title: String, imageName: String) {
        
        // title 从内向外设置的
        control.title = title
        
        // 设置文本属性 NSAttributedString.h
        // 文本属性的 Key 都包含 AttributeName
        control.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        // 如果要设置文字大小，需要设置 Normal 状态
        control.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(10)], forState: .Normal)

        control.tabBarItem.image = UIImage(named: imageName)
        // 默认情况下，tabbar的image会使用系统默认颜色`渲染`
        // AlwaysOriginal 渲染模式，就会直接使用平面设计师提供的素材颜色，系统不再加工
        control.tabBarItem.selectedImage = UIImage(named:imageName+"_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        let nav = UINavigationController(rootViewController: control)
        self.addChildViewController(nav)

   }
    
    func addControllers() {
        
        addChildViewController(HomeController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageController(), title: "消息", imageName: "tabbar_message_center")
        
        // 添加一个空白的控制器
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileController(), title: "我", imageName: "tabbar_profile")
    }
    
}
